package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-05
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SubjectData.class, new SubjectExcelListener(this)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubjects() {
        // 查询所有一级二级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0L);

        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0L);

        // 根据wrapper中的条件，使用映射查询。
//        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        // 或者使用这种方法
        List<EduSubject> listOne = this.list(wrapperOne);
        List<EduSubject> listTwo = this.list(wrapperTwo);

//        log.info("listOne " + listOne.toString());
//        log.info("listTwo " + listTwo.toString()); // 这个是有值得，可是0L居然也不等于

        // 封装一级二级分类
        ArrayList<OneSubject> finalList = new ArrayList<>();

        for (EduSubject oneItem : listOne) {
            OneSubject oneSubject = new OneSubject();
            ArrayList<TwoSubject> tempListTwoSubjects = new ArrayList<>();

            BeanUtils.copyProperties(oneItem, oneSubject);

            for (EduSubject twoItem : listTwo) {
                if (oneItem.getId().toString().equals(twoItem.getParentId().toString())) {

                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoItem, twoSubject);

                    tempListTwoSubjects.add(twoSubject);
                }
            }

            if (tempListTwoSubjects.size() <= 0) continue;

            oneSubject.setChildren(tempListTwoSubjects);

            finalList.add(oneSubject);
        }

        // 自己写的笨方法
//        for (EduSubject oneItem : listOne) {
//            OneSubject temp = new OneSubject();
//
//            temp.setId(oneItem.getId());
//            temp.setTitle(oneItem.getTitle());
//
//            for (EduSubject twoItem : listTwo) {
//                if (twoItem.getParentId().equals(oneItem.getId())) {
//                    List<TwoSubject> tempChildren = temp.getChildren();
//
//                    TwoSubject tempTwoSubject = new TwoSubject();
//                    tempTwoSubject.setId(twoItem.getId());
//                    tempTwoSubject.setTitle(twoItem.getTitle());
//
//                    tempChildren.add(tempTwoSubject);
//
//                    temp.setChildren(tempChildren);
//                }
//            }
//
//            finalList.add(temp);
//        }

        return finalList;
    }
}
